package com.yb.guigu.pick.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/6/14 19:03
 * FileName: SHA256Util
 * Description:
 */
@Slf4j
public class SHA256Util {

    private static final String SHA256_ALGORITHM_NAME = "SHA-256";

    public static MessageDigest digest(){
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance(SHA256_ALGORITHM_NAME);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return messageDigest;
    }


    private static  String convert(byte[] result){
        StringBuilder hexString = new StringBuilder();
        for (byte b : result) {
            hexString.append(String.format("%02X", b));
        }

        return hexString.toString().toLowerCase();
    }
    public static String digest(InputStream is){
        String sha256 = "";
        try {
            MessageDigest messageDigest = digest();
            final byte[] buffer = new byte[StreamUtils.BUFFER_SIZE];
            int bytesRead = -1;
            while ((bytesRead = is.read(buffer)) != -1) {
                messageDigest.update(buffer, 0, bytesRead);
            }
            byte[] result = messageDigest.digest();
            return convert(result);
        } catch (IOException e) {
            log.error(String.valueOf(e));
        }
        return sha256;
    }

    public static String digest(String origin){

        MessageDigest messageDigest = digest();
        messageDigest.update(origin.getBytes(StandardCharsets.UTF_8));

        return convert(messageDigest.digest());
    }


    public static String digest(CharSequence origin){
        String md5 = "";

        MessageDigest messageDigest = digest();

        messageDigest.update(origin.toString().getBytes(StandardCharsets.UTF_8));

        return convert(messageDigest.digest());
    }

    public static void main(String[] args) {
        int cur=0;
        while (cur++<10){
            doDraw();
        }

    }

    public static void doDraw(){
        List<Integer> resultList = draw(10000);

        Map<Integer, Long> map = resultList.stream().collect(Collectors.groupingBy((item) -> item,Collectors.counting()));
        map.forEach((key,value)->{
            System.out.print(String.format("%s->%s",arr2[key],value)+":");
        });
        System.out.println();
    }
    static String arr2[]=new String[]{"一等奖","二等奖","三等奖","很遗憾"};

    static double arr[]=new double[]{0.01,5,10,85.8};
    public static int draw(){
            // 一等奖 1%
            // 二等奖 5%
            // 三等奖 10%
            // 不中 84%

        Random random=new Random();
        double randomP = random.nextDouble()*100;
        for(int i=0;i<arr.length;i++){
            if(randomP<arr[i]){
                return i;
            }else{
                randomP-=arr[i];
            }
        }
        return arr.length-1;

    }

    public static List<Integer>  draw(int count){
        int index=0;
        List<Integer> result=new ArrayList<>();
        while(index<count){
            result.add(draw());
            index++;
        }
        return result;
    }
}
